package edu.uwm.univesitydb.mock;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import edu.uwm.universitydb.swing.DataTableModel;


public class MockStudentService extends DataTableModel {

	public MockStudentService() {
		super(_colNames, _colTypes);
	}

	public boolean addStudent(String ssn, String name, int age, String degree, Dept dept ) {
		return _students.add(new Student(ssn, name, age, degree, dept));
	}
	
	public boolean updateStudent(String ssn, String name, int age, String degree, Dept dept) {
		Student student = getStudentByID(ssn);
		if(student != null) {
			student._name = name;
			student._age = age;
			student._degree = degree;
			student._dept = dept;
			return true;
		} else {
			return false;
		}
	}
	public boolean deleteByID(String ssn) {
		Student match = null;
		for(Student stud: _students) {
			if(stud._ssn.equals(ssn)) {
				match = stud;
				break;
			}
		}
		return (match == null) ? false : _students.remove(match);
	}

	@Override
	public int getRowCount() {
		return _students.size();
	}
	@Override
	public Object getValueAt(int row, int col) {
		switch (col) {
		case 0:
			return _students.get(row)._ssn;
		case 1:
			return _students.get(row)._name;
		case 2:
			return _students.get(row)._age;
		case 3:
			return _students.get(row)._degree;
		case 4:
			return _students.get(row)._dept._name;
		default:
			return "Error";
		}
	}
	
	public String getSSNByID(String id) {
		Student student = getStudentByID(id);
		if (student != null) {
			return student._ssn;
		} else {
			return "";
		}
	}
	
	public String getNameByID(String id) {
		Student student = getStudentByID(id);
		if (student != null) {
			return student._name;
		} else {
			return "";
		}
	}
	
	public int getAgeByID(String id) {
		Student student = getStudentByID(id);
		if (student != null) {
			return student._age;
		} else {
			return -1;
		}
	}
	public int getStudentDeparmentByID(String id) {
		Student student = getStudentByID(id);
		if (student != null) {
			return student._dept._id;
		} else {
			return -1;
		}
	}
	public String getDegreeByID(String id) {
		Student student = getStudentByID(id);
		if (student != null) {
			return student._degree;
		} else {
			return "";
		}
	}
	
	public Dept getDepartmentByID(int id) {
		for (Dept dept : _depts) {
			if (dept._id == id) {
				return dept;
			}
		}
		return null;
	}

	private Student getStudentByID(String id) {
		for (Student stud : _students) {
			if (stud._ssn.equals(id)) {
				return stud;
			}
		}
		return null;
	}

	public Dept[] getDepartments() {
		return _depts.toArray(new Dept[_depts.size()]);
	}
	
	public static class Student {
		public String _ssn;
		public String _name;
		public int _age;
		public String _degree;
		public Dept _dept;
		public Student(String ssn, String name, int age, String degree, Dept dept) {
			_ssn = ssn;
			_name = name;
			_age = age;
			_degree = degree;
			_dept = dept;
		}
	}
	
	public static class Dept {
		public int _id;
		public String _name;
		public Dept(int id, String name) {
			_id = id;
			_name = name;
		}
		public String toString() {
			return _name;
		}
		public boolean equals(Object o) {
			return (o instanceof Dept && ((Dept)o)._id == this._id);
		}
	}
	
	private static final String[] _colNames = new String[] {"SSN","Name", "Age", "Degree", "Dept"};
	private static final Class<?>[] _colTypes = new Class<?>[] {String.class, String.class, Integer.class, String.class, Dept.class};
	private static final long serialVersionUID = 8523394124940086466L;
	
	
	private static List<Dept> _depts = Arrays.asList(new Dept[] {
			new Dept(1,"Computer Science"),
			new Dept(2,"Electrical Engineering"),
			new Dept(3,"Civil Engineering")
	});
	private static List<Student> _students = new ArrayList<Student>(Arrays.asList(new Student[] {
			new Student("398-58-5538", "Alex", 33, "Computer Science M.S", _depts.get(0)),
			new Student("509-76-6667", "Blake",	28, "Computer Science M.S", _depts.get(0)),
			new Student("234-59-5745", "Bob", 32, "Computer Science M.S", _depts.get(0)),
			new Student("687-41-3864", "Kristin", 23, "Engineering M.S", _depts.get(2)),
			new Student("745-56-5465", "Rick", 35, "Engineering M.S", _depts.get(2)),
			new Student("118-70-3480", "Ted", 21, "Computer Science M.S", _depts.get(0)),
			new Student("623-17-9702", "Johnny", 36, "Computer Science M.S", _depts.get(0)),
			new Student("715-12-2287", "Abel", 44, "Computer Science M.S", _depts.get(0)),
			new Student("778-27-5374", "Sophie", 20, "Engineering M.S", _depts.get(1)),
			new Student("259-52-5452", "Barry", 25, "Engineering M.S", _depts.get(1)),
			new Student("396-76-0296", "Alex", 33, "Computer Science M.S", _depts.get(0)),
			new Student("825-76-2373", "Blake",	28, "Computer Science M.S", _depts.get(0)),
			new Student("620-52-9119", "Bob", 32, "Computer Science M.S", _depts.get(0)),
			new Student("113-80-3928", "Kristin", 23, "Computer Science M.S", _depts.get(0)),
			new Student("950-69-8522", "Rick", 35, "Computer Science M.S", _depts.get(0)),
			new Student("428-64-4419", "Ted", 21, "Engineering M.S", _depts.get(2)),
			new Student("746-77-3682", "Johnny", 36, "Computer Science M.S", _depts.get(0)),
			new Student("127-42-5258", "Abel", 44, "Computer Science M.S", _depts.get(0)),
			new Student("637-34-0402", "Sophie", 20, "Engineering M.S", _depts.get(1)),
			new Student("732-26-3403", "Barry", 25, "Engineering M.S", _depts.get(2)),
			new Student("367-81-9615", "Alex", 33, "Computer Science M.S", _depts.get(0)),
			new Student("700-25-7544", "Blake",	28, "Computer Science M.S", _depts.get(0)),
			new Student("728-43-4588", "Bob", 32, "Computer Science M.S", _depts.get(0)),
			new Student("857-19-8465", "Kristin", 23, "Engineering M.S", _depts.get(1)),
			new Student("509-88-2443", "Rick", 35, "Engineering M.S", _depts.get(1)),
			new Student("351-89-2542", "Ted", 21, "Computer Science M.S", _depts.get(0)),
			new Student("786-97-7102", "Johnny", 36, "Computer Science M.S", _depts.get(0)),
			new Student("380-58-5874", "Abel", 44, "Computer Science M.S", _depts.get(0)),
			new Student("440-43-0624", "Sophie", 20, "Engineering M.S", _depts.get(1)),
			new Student("956-52-9127", "Barry", 25, "Engineering M.S", _depts.get(2)),
			new Student("766-86-0187", "Joseph", 30, "Engineering M.S", _depts.get(2))
	}));
}
